package com.lishe.lishe.lishe.module.excitation.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.module.excitation.bean.WelfareObject;
import com.lishe.lishe.lishe.utils.BitmapUtils;
import com.lishe.lishe.lishe.utils.PermissionUtils;
import com.lishe.lishe.lishe.utils.UMShareUtils;
import com.lishe.lishe.wxapi.WXEntryActivity;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;


/**
 * Created by shino on 2017/12/12.
 * 福利分享
 */

public class WelfareShareActivity extends BaseActivity implements View.OnClickListener {

    private TextView title;
    private Button callBack;
    private Button share;

    private ImageView giftImage;
    private ImageView comapanyImage;
    private TextView name;
    private TextView comname;
    private TextView company;
    private TextView gift;
    private ScrollView scrollView;

    private ImageLoaderUtils imageLoaderUtils;
    private WelfareObject welfareObject;

    public static void jump(Activity activity, WelfareObject welfareObject) {
        Intent intent = new Intent(activity, WelfareShareActivity.class);
        intent.putExtra("welfareObject", welfareObject);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welfare_share);
        imageLoaderUtils = new ImageLoaderUtils();
        welfareObject = (WelfareObject) getIntent().getSerializableExtra("welfareObject");
        registerBroad();
        initView();
    }

    private void initView() {
        title = findViewById(R.id.title_text_title);
        callBack = findViewById(R.id.title_left_button);
        share = findViewById(R.id.title_rigth_button);
        share.setBackgroundResource(R.mipmap.share_icon);
        share.setVisibility(View.VISIBLE);
        title.setText("福利分享");

        scrollView = findViewById(R.id.scrollView);
        giftImage = findViewById(R.id.center_image);
        comapanyImage = findViewById(R.id.company_image);
        name = findViewById(R.id.name);
        comname = findViewById(R.id.comname);
        company = findViewById(R.id.company);
        gift = findViewById(R.id.gift);
        initData();
        setListener();
    }

    private void initData() {
        if (welfareObject != null) {
            name.setText(welfareObject.getUserName() + "刚刚获得");
            company.setText(welfareObject.getComName() + "送出");
            if(TextUtils.isEmpty(welfareObject.getComLogoPath())&&!TextUtils.isEmpty(welfareObject.getComName())){
                comname.setVisibility(View.VISIBLE);
                comapanyImage.setVisibility(View.GONE);
                comname.setText(welfareObject.getComName());
            }else{
                comname.setVisibility(View.GONE);
                comapanyImage.setVisibility(View.VISIBLE);
                imageLoaderUtils.displayImage(welfareObject.getComLogoPath(), comapanyImage);
            }
            if (welfareObject.getWelfareForm().equals("0")) {//福利积分
                imageLoaderUtils.displayImage(welfareObject.getImgUrl(), giftImage);
            } else {//福利礼包
                imageLoaderUtils.displayImage(welfareObject.getPackageImg(), giftImage);
            }
            gift.setText("~" + welfareObject.getWelfareName() + "关怀~");
        }
    }

    private void setListener() {
        callBack.setOnClickListener(this);
        share.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left_button:
                finish();
                break;
            case R.id.title_rigth_button:
                if (PermissionUtils.isStorageWritePermission(WelfareShareActivity.this)) {
                    UMShareUtils.getInstance().sharedImageBitmapMessage(WelfareShareActivity.this, "", BitmapUtils.shotScrollView(scrollView), umShareListener);
                }
                break;
        }
    }

    private String share_media;
    private UMShareListener umShareListener = new UMShareListener() {

        @Override
        public void onStart(SHARE_MEDIA share_media) {
            WelfareShareActivity.this.share_media = share_media.toString();
        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {
            String channel = null;
            if (share_media.toString().equals("QZONE")) {
                channel = "QQShare";
                toast("成功分享到QQ空间");
            } else if (share_media.toString().equals("QQ")) {
                channel = "QQChat";
                toast("成功分享到QQ");
            }
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            String message = throwable.getMessage();
            toast(subMessage(message));
        }

        /**
         * 获取错误信息
         * @param message
         */
        private String subMessage(String message) {
            int start = message.indexOf("错误信息：");
            int end = message.indexOf("点击查看错误");
            return message.substring(start == -1 ? 0 : start, end == -1 ? message.length() : end);
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            toast("取消分享");
        }
    };

    private void registerBroad() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(WXEntryActivity.action_success);
        filter.addAction(WXEntryActivity.action_cancle);
        filter.addAction(WXEntryActivity.action_faild);
        registerReceiver(receiver, filter);
    }

    /**
     * 接收微信回调WXEntryActivity发送的广播
     */
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()) {
                case WXEntryActivity.action_success:
                    String channel;
                    if (share_media.equals("WEIXIN")) {
                        channel = "WeChat";
                        toast("成功分享到微信");
                    } else {
                        channel = "WeShare";
                        toast("成功分享到微信朋友圈");
                    }
                    break;
                case WXEntryActivity.action_cancle:
                    toast("取消分享");
                    break;
                case WXEntryActivity.action_faild:
                    toast("分享失败");
                    break;
            }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(WelfareShareActivity.this).onActivityResult(requestCode, resultCode, data);
    }

}
